package com.ys.controller.api.amazonorder;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.entity.amazonorder.TcoDayShop;
import com.ys.entity.amazonorder.TpAmazonShopOrderDaily;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.DateUtil;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/**
 * 店铺日报表
 * @author XDH
 *
 */
@Controller
@RequestMapping("/api/inter/tcoDayShop")
public class ApiInterTcoDayShopController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TcoDayShop> commonApiService;

	@RequiresPermissions("tcoDayShop:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(
			@ModelAttribute TcoDayShop cond){

		cond = commonApiService.dataAuth(MenuId.TCO_DayShop, cond);
		cond.addField("R.RankToday AS rankToday");
		cond.addLeftJoin("kjecp_report.dbo.TCO_RankShopByAmountRmb R ON R.ShopId = A.ShopId AND R.CountDate = A.TimeDay");
		cond.setOrderby("A.TimeDay desc,A.NumOrder desc");
		
		LayuiTableVoJA vo = commonApiService.searchLay(cond);
		{
			JSONObject JO = new JSONObject();
			
			// 订单数累计
			JO.put("numOrder", cond.setFields("ISNULL(SUM(NumOrder),0)").sumDecimal());
			
			// 订单总金额累计
			JO.put("amountLocal", cond.setFields("ISNULL(SUM(AmountLocal),0)").sumDecimal());
			
			// 订单总金额累计
			JO.put("amountRmb", cond.setFields("ISNULL(SUM(AmountRmb),0)").sumDecimal());
			
			vo.setTotalRow(JO);
		}
		
		return vo;
	}
	
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcoDayShop cond, String conditions) throws Exception{

		cond = commonApiService.dataAuth(MenuId.TCO_DayShop, cond);
		
		cond.setPage(0).setSize(999999);
		
		String timeDay = ConditionUtil.getConditionValue(conditions, "timeDay");
		
		if (timeDay != null && timeDay.length() > 0) {
			String[] timeDays = timeDay.split(" ~ ");
			cond.where("A.TimeDay >= '" + timeDays[0] + "' and A.TimeDay <= '" + timeDays[1] + "'");
		} else {
			Date now = new Date();
			cond.where("A.TimeDay >= '" + DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1)) + "' and A.TimeDay <= '" + DateUtil.getYYYY_MM_DD(now) + "'");
		}

		cond.addField("R.RankToday AS rankToday");
		cond.addLeftJoin("kjecp_report.dbo.TCO_RankShopByAmountRmb R ON R.ShopId = A.ShopId AND R.CountDate = A.TimeDay");

		cond.setOrderby("A.TimeDay desc,A.NumOrder desc");

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TCO_DayShop)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TpAmazonShopOrderDaily> util = new ExportExcelUtils<TpAmazonShopOrderDaily>(new TpAmazonShopOrderDaily());
        
        String toFilePath = util.exportExcel(listData, listField, "订单日报表");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }
}
